package com.yaorange.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaorange.api.Result;
import com.yaorange.entity.Book;
import com.yaorange.service.BookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @description: 书籍处理器
 * @company: yaorange
 * @author: Mr.Liu
 * @version: 1.0
 * @create: 2021-11-23 15:56:10
 */
@RestController
public class BookController {
    @Resource
    private BookService bookService;

    @RequestMapping("/book/list")
    public List<Book> getAll(){
        List<Book> bookList = bookService.findAll();
        return bookList;
    }

    @GetMapping("/page/{pageNum}/{pageSize}")
    public Result<PageInfo<Book>> getPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize){

        Result<PageInfo<Book>> result = new Result<>();
        PageInfo<Book> pageInfo = bookService.page(pageNum, pageSize);
        result.setCode(200);
        result.setSuccess(true);
        result.setMessage("分页查询成功");
        result.setData(pageInfo);
        return result;
    }

    @PostMapping("/book")
    public Result<?> add(@RequestBody Book book){
        System.out.println(book);
        Result<Object> result = new Result<>();
        Boolean aBoolean = bookService.add(book);
        if (aBoolean){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("新增成功");
            return result;
        }else {
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("新增失败");
            return result;
        }
    }

    @PutMapping("/book")
    public Result<?> update(@RequestBody Book book){
        Result<Object> result = new Result<>();
        Boolean aBoolean = bookService.update(book);
        if (aBoolean){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("修改成功");
            return result;
        }else {
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("修改失败");
            return result;
        }
    }

    @DeleteMapping("/book/{id}")
    public Result<?> delete(@PathVariable Integer id){
        Result<Object> result = new Result<>();
        Boolean aBoolean = bookService.delete(id);
        if (aBoolean){
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("删除成功");
            return result;
        }else {
            result.setCode(400);
            result.setSuccess(false);
            result.setMessage("删除失败");
            return result;
        }
    }

    @RequestMapping("/book/select/{id}")
    public Result<?> selcet(@PathVariable("id") Integer id){
        Result<Object> result = new Result<>();
        Book book = bookService.select(id);
        result.setCode(200);
        result.setSuccess(true);
        result.setMessage("查询成功");
        System.out.println("修改");
        System.out.println("远程");
        result.setData(book);
        return result;
    }
}
